package com.dhcc.DRGsHosp.application.impl;

import java.util.List;
import java.util.Set;

import javax.inject.Named;

import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.QueryChannelService;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.EsIndexApplication;
import com.dhcc.DRGsHosp.core.domain.EsIndex;

@Named
@Transactional
public class EsIndexApplicationImpl implements EsIndexApplication{

   private QueryChannelService queryChannel;
	
    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
	
	
	@Override
	public EsIndex getEsIndex(String salesId) {
		return EsIndex.get(EsIndex.class,salesId);
	}

	@Override
	public void creatEsIndex(EsIndex esd) {
		esd.save();
		
	}

	@Override
	public void updateEsIndex(EsIndex esd) {
		esd.save();
		
	}
   
	@Override
	public void removeEsIndex(EsIndex esd) {
		if(esd!=null) {
			esd.remove();
		}
		
	}
	
	
	@Override
	public void removeEsIndexs(Set<EsIndex> esds) {
		for(EsIndex esd:esds) {
			if(esd!=null) {
				esd.remove();
			}
		}
		
	}

	
	/**  
	 *  @Description：获取全部ES服务信息
	 *  @Author huangju    
	 */
	
	@Override
	public List<EsIndex> findAllEsIndex() {
		return EsIndex.findAll(EsIndex.class);
	}

	
	/**
     *  
     *  isSalesIdExist.  
     *  @Description：系统使用es的业务ID是否存在
     *  @Author huangju
     *  @param salesId
     *  @return 
     *  @Date:2018年10月30日
     */
	@Override
	public boolean isSalesIdExist(String esIndex, String salesName) {
		String sql="select count(1) from drgs.T_ESINDEX t where 1=1 ";
		if(esIndex!=null) {
			sql+=" and  t.es_index = '"+esIndex+"'";
		}
		if(salesName!=null) {
			sql+=" and  t.sales_name = '"+salesName+"'";
		}
		Integer count = Integer.parseInt(getQueryChannelService().createSqlQuery(sql).singleResult().toString()) ;
		if(count>0) {
			return true;
		}
		return false;
	}


	

}
